import os
import pandas as pd
from API.okex import get_usdtcny_premium_okex, get_usdtcny_orderbook_okex
from utils import df_into_db, save_to_mysql


DATA_DIR = 'E:\\data'
PREMIUM_DATA_DIR = os.path.join(DATA_DIR, 'usdt_premium')
orderbook_file_name = os.path.join(PREMIUM_DATA_DIR, f'usdtcny_orderbook_okex.csv')
orderbook = get_usdtcny_orderbook_okex()
if os.path.exists(orderbook_file_name):
    historical_orderbook = pd.read_csv(orderbook_file_name)
    df_ob = pd.concat([historical_orderbook, orderbook], axis=0, ignore_index=True)
    df_ob.drop_duplicates(subset=['datetime', 'id'], keep='first', inplace=True)
    df_ob.fillna(0, inplace=True)
    save_to_mysql(df_ob, 'usdt_premium', table_name='usdtcny_orderbook_okex2')
    df_into_db(df_ob, db_name='usdt_premium', table_name='usdtcny_orderbook_okex')
    # df_ob.to_csv(orderbook_file_name, index=False)
else:
    # orderbook.to_csv(file_name, index=False)
    pass